package com.yylending.las.lending.quota;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.Loader;
import android.view.KeyEvent;
import android.view.MenuItem;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yylending.las.R;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.common.location.entity.Location;
import com.yylending.las.lending.quota.baseinfo.InfoFragment;
import com.yylending.las.lending.quota.credit.CreditAuthFragment;
import com.yylending.las.lending.quota.credit.CreditAuthLimitedFragment;
import com.yylending.las.lending.quota.face.FaceQuotaFragment;
import com.yylending.las.lending.quota.idcard.IdCardInfoConfirmFragment;
import com.yylending.las.lending.quota.idcard.IdCardInfoFragment;
import com.yylending.las.lending.quota.idcard.IdCardScanFragment;
import exocr.engine.EngineManager;

/**
 * Created by HenryZhang on 2016/11/8.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class ApplyQuotaActivity extends BaseActivity {
	private final static String TAG = "ApplyQuotaActivity";

	private final static String ARG_LOCATION = "arg_location";

	private final static String INFO_TAG = "info_fragment";
	private final static String IDENTITY_TAG = "identity_fragment";
	private final static String FACE_TAG = "face_fragment";
	private final static String CREDIT_TAG = "credit_fragment";
	private final static String ID_CARD_INFO_TAG = "idcard_info_fragment";
	private final static String ID_CARD_CONFIRM_TAG = "idcard_confirm_fragment";
	private final static String CREDIT_AUTH_LIMITED_TAG = "credit_auth_limited_fragment";
	public final static int PAGE_INFO = 1;
	public final static int PAGE_ID_CARD_SCAN = 2;
	public final static int PAGE_FACE = 3;
	public final static int PAGE_CREDIT = 4;
	public final static int PAGE_ID_CARD_INFO = 5;
	public final static int PAGE_ID_CARD_CONFIRM = 6;
	public final static int PAGE_ID_CREDIT_AUTH_LIMITED = 7;
	private final static String KEY_CURRENT_PAGE = "current_page";
	private final static String KEY_QUOTA_OPERATE_ID = "quota_operateid";

	private Location mLocation;

	private boolean mHadAreaInit = false;
	private boolean mHadOptionInit = false;
	private int mCurrentPage = PAGE_INFO;

	private String mTitle = "";

	public static void start(Context aty, Location location) {
		Intent i = new Intent(aty, ApplyQuotaActivity.class);
		i.putExtra(ARG_LOCATION, location);
		aty.startActivity(i);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

	}

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_apply_quota);
		//setTitleBarColor(R.color.color_top_bg_blue);
		mLocation = getIntent().getParcelableExtra(ARG_LOCATION);
		mTitle = getString(R.string.title_activity_get_quota);
		setTitle(mTitle);
		if (null != savedInstanceState) {
			restoreData(savedInstanceState);
		}
		showFragment(mCurrentPage, null);
	}
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(KEY_CURRENT_PAGE, mCurrentPage);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		//restoreData(savedInstanceState);
	}

	private void restoreData(Bundle savedInstanceState) {
		if (null != savedInstanceState) {
			mCurrentPage = savedInstanceState.getInt(KEY_CURRENT_PAGE);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				judgePage();
				break;
			default:
				CLog.i(TAG, "click view id=" + item.getItemId());
				break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void showFragment(int page, Object param) {
		mTitle = getString(R.string.title_activity_get_quota);

		CLog.i(TAG, "showFragment page=" + page);
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		/*ft.setCustomAnimations(
				R.anim.slide_right_in, R.anim.slide_left_out,
				R.anim.slide_left_in, R.anim.slide_right_out);*/
		Fragment infoFragment = fm.findFragmentByTag(INFO_TAG);
		Fragment identityFragment = fm.findFragmentByTag(IDENTITY_TAG);
		Fragment faceFragment = fm.findFragmentByTag(FACE_TAG);
		Fragment creditFragment = fm.findFragmentByTag(CREDIT_TAG);
		Fragment idCardInfoFragment = fm.findFragmentByTag(ID_CARD_INFO_TAG);
		Fragment idCardConfirmFragment = fm.findFragmentByTag(ID_CARD_CONFIRM_TAG);
		Fragment creditAuthLimitedFragment = fm.findFragmentByTag(CREDIT_AUTH_LIMITED_TAG);
		if (infoFragment != null) {
			ft.hide(infoFragment);
		}
		if (identityFragment != null) {
			ft.hide(identityFragment);
		}
		if (faceFragment != null) {
			ft.hide(faceFragment);
		}
		if (creditFragment != null) {
			ft.hide(creditFragment);
		}
		if (idCardInfoFragment != null) {
			ft.remove(idCardInfoFragment);
		}
		if (idCardConfirmFragment != null) {
			ft.remove(idCardConfirmFragment);
		}
		if (creditAuthLimitedFragment != null){
			ft.remove(creditAuthLimitedFragment);
		}
		switch (page) {
			case PAGE_INFO:
				if (infoFragment == null) {
					infoFragment = InfoFragment.newInstance(page, mLocation);
					ft.add(R.id.apply_quota_container, infoFragment, INFO_TAG);
				} else {
					ft.show(infoFragment);
				}
				mCurrentPage = page;
				break;
			case PAGE_ID_CARD_SCAN:
				if (identityFragment == null) {
					identityFragment = IdCardScanFragment.newInstance(page);
					ft.add(R.id.apply_quota_container, identityFragment, IDENTITY_TAG);
				} else {
					ft.show(identityFragment);
				}
				mCurrentPage = page;
				break;
			case PAGE_FACE:
				if (faceFragment == null) {
					faceFragment = FaceQuotaFragment.newInstance(page);
					ft.add(R.id.apply_quota_container, faceFragment, FACE_TAG);
				} else {
					ft.show(faceFragment);
				}
				mCurrentPage = page;
				break;
			case PAGE_CREDIT:
				if (creditFragment == null) {
					creditFragment = CreditAuthFragment.newInstance(page);
					ft.add(R.id.apply_quota_container, creditFragment, CREDIT_TAG);
				} else {
					ft.show(creditFragment);
				}
				mCurrentPage = page;
				break;
			case PAGE_ID_CARD_INFO:
				idCardInfoFragment = IdCardInfoFragment.newInstance(page, param);
				ft.add(R.id.apply_quota_container, idCardInfoFragment, ID_CARD_INFO_TAG);
				mCurrentPage = page;
				break;
			case PAGE_ID_CARD_CONFIRM:
				idCardConfirmFragment = IdCardInfoConfirmFragment.newInstance(page, param);
				ft.add(R.id.apply_quota_container, idCardConfirmFragment, ID_CARD_CONFIRM_TAG);
				mCurrentPage = page;
				break;
			case PAGE_ID_CREDIT_AUTH_LIMITED:
				mTitle = getString(R.string.credit_auth_commit_failed);
				creditAuthLimitedFragment = CreditAuthLimitedFragment.newInstance(page);
				ft.add(R.id.apply_quota_container, creditAuthLimitedFragment, CREDIT_AUTH_LIMITED_TAG);
				mCurrentPage = page;
				break;
			default:
				break;
		}

		setTitle(mTitle);
		CLog.i(TAG, "当前页-->" + mCurrentPage);
		ft.commitAllowingStateLoss();
	}

	/*@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (mCurrentPage) {
			case PAGE_INFO: {
				FragmentManager fm = getSupportFragmentManager();
				Fragment fragment = fm.findFragmentByTag(INFO_TAG);
				fragment.onActivityResult(requestCode, resultCode, data);
				break;
			}
			case PAGE_CREDIT: {
				FragmentManager fm = getSupportFragmentManager();
				Fragment fragment = fm.findFragmentByTag(CREDIT_TAG);
				fragment.onActivityResult(requestCode, resultCode, data);
				break;
			}
		}
	}*/

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			judgePage();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	public int getmCurrentPage() {
		return mCurrentPage;
	}

	public void setmCurrentPage(int mCurrentPage) {
		this.mCurrentPage = mCurrentPage;
	}

	@Override
	protected void onDestroy() {
		CLog.i(TAG, "onDestroy()--");
		EngineManager.getInstance().finishEngine();
		super.onDestroy();
	}

	/**
	 * 界面跳转判断
	 */
	private void judgePage() {
		finish();
		/*switch (mCurrentPage) {
			case PAGE_INFO:
				finish();
				break;
			case PAGE_ID_CARD_SCAN:
				EventAgent.post(new IdCardPresenter.Event(IdCardPresenter.Event.ID_CARD_RESCAN));
				showFragment(PAGE_INFO, null);
				break;
			case PAGE_FACE:
				showFragment(PAGE_ID_CARD_CONFIRM, null);
				break;
			case PAGE_CREDIT:
				showFragment(PAGE_FACE, null);
				break;
			case PAGE_ID_CARD_INFO:
				showFragment(PAGE_ID_CARD_SCAN, null);
				break;
			case PAGE_ID_CARD_CONFIRM:
				showFragment(PAGE_INFO, null);
				break;
		}*/
	}
}
